//
//  UserViewController.m
//  Drops
//
//  Created by dllo on 16/6/2.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "UserViewController.h"
#import "UserTableViewCell.h"
#import "LoginViewController.h"
#import "AFNetworking.h"
#import "UIKit+AFNetworking.h"
#import "UIImageView+WebCache.h"
#import <BmobSDK/Bmob.h>
@interface UserViewController ()<UITableViewDelegate,UITableViewDataSource,LoginViewControllerDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIButton *leftButton;
@property(nonatomic, strong)UIButton *rightButton;
@property(nonatomic, strong)UIImageView *headImage;
@property(nonatomic, strong)UILabel *userLabel;
@property(nonatomic, strong)UIButton *changeImage;
@property(nonatomic, strong)NSMutableArray *imageArr;
@property(nonatomic, strong)NSMutableArray *imageDateArr;
@property (nonatomic, retain)UIImageView *imageV;
@end

@implementation UserViewController

-(void)viewWillAppear:(BOOL)animated{
    self.imageArr = [NSMutableArray array];
    self.imageDateArr = [NSMutableArray array];
    [self creatData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatView];
    
}
-(void)creatView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    self.imageV= [[UIImageView alloc]initWithFrame:CGRectMake(0, -300, self.view.frame.size.width, 300)];
//        imageV.image = [UIImage imageNamed:@"08.jpg"];
    self.imageV.backgroundColor = Z_BLUECOLOR;
    //self.tableView.tableHeaderView = imageV;
    self.imageV.tag = 100;
    [self.tableView addSubview:self.imageV];
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton.frame = CGRectMake(0, 20, 40, 40);
    [self.leftButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    self.leftButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.leftButton];
    [self.leftButton addTarget:self action:@selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    self.headImage.center = CGPointMake(WIDTH / 2, 120);
    self.headImage.layer.cornerRadius = 40;
    self.headImage.layer.borderWidth = 2;
    self.headImage.backgroundColor = [UIColor whiteColor];
//    self.headImage.layer.borderColor = [UIColor grayColor].CGColor;
    self.headImage.layer.borderColor = [UIColor whiteColor].CGColor;
    self.headImage.layer.masksToBounds = YES;
//    self.headImage.image = [UIImage imageNamed:@"userzhanwei.jpg"];
    self.headImage.image = [UIImage imageNamed:@"userHead.png"];
//    self.headImage.backgroundColor = Z_BLUECOLOR;
    [self.imageV addSubview:self.headImage];
    self.userLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    self.userLabel.center = CGPointMake(WIDTH / 2, 190);
    self.userLabel.textAlignment = 1;
    self.userLabel.text = @"没有用户登录";
    self.userLabel.textColor = [UIColor whiteColor];
    [self.imageV addSubview:self.userLabel];
    
    self.changeImage = [UIButton buttonWithType:UIButtonTypeCustom];
    self.changeImage.frame = CGRectMake(0, 0, 80, 120);
    self.changeImage.center = CGPointMake(WIDTH / 2, 140);
    self.changeImage.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.changeImage];
    [self.changeImage addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)creatData{
    dispatch_queue_t myQueue = dispatch_queue_create("zhaoyutian ", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(myQueue, ^{
        BmobQuery *bquery = [BmobQuery queryWithClassName:@"image"];
        bquery.limit = 1000;
        [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            for (BmobObject *obj in array) {
                NSString *str = [obj objectForKey:@"updatedAt"];
                [self.imageDateArr addObject:str];
                BmobFile *file = [obj objectForKey:@"image"];
                if (file != nil) {
                    [self.imageArr addObject:file.url];
                }
            }
        }];
        dispatch_async(mainQueue, ^{
          
            
        });
        
    });
    }

-(void)leftButton:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
-(void)changeImage:(UIButton *)button{
        LoginViewController  *vc = [[LoginViewController alloc]init];
        vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:vc animated:YES completion:^{
            vc.delegate = self;
            
        }];
}
-(void)sendValue:(NSArray *)arr{
    self.imageV.image = [UIImage imageNamed:@"08.jpg"];
    if (arr.count == 2) {
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", arr[1]]]];
        self.userLabel.text = arr[0];
    }else{
        self.userLabel.text = arr[0];
    }
    [self.tableView reloadData];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //以后当页面里同时存在tableview和scrollview时,要事先判断当前是哪个控件触发的协议方法,避免因为没有判断,造成错误的操作.
    CGFloat yOffest = self.tableView.contentOffset.y;
    UIImageView *imageV= (UIImageView *)[self.view viewWithTag:100];
    if (yOffest < 0) {
        imageV.frame = CGRectMake(0, yOffest, self.view.frame.size.width, -yOffest);
    }
    if (yOffest > -190) {
        self.headImage.hidden = YES;
        self.userLabel.hidden = YES;
        self.changeImage.hidden = YES;
    }else{
        self.headImage.hidden = NO;
        self.userLabel.hidden = NO;
        self.changeImage.hidden = NO;

    }
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld",self.imageArr.count);
    return self.imageArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageArr[indexPath.row]]];
    UIImage *image = [UIImage imageWithData:data];
    cell.image.image = image;
    //[cell.image setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.row]]];
     cell.user.text = self.imageDateArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageArr[indexPath.row]]];
//    UIImage *image = [UIImage imageWithData:data];
//    CGFloat height = self.view.frame.size.width * image.size.height / image.size.width;
    return 300;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
